package com.moyun.zbmy.youxian.ui.activities;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.content.ContextCompat;
import android.support.v4.util.ArrayMap;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.canyinghao.canrefresh.CanRefreshLayout;
import com.canyinghao.canrefresh.other.CanRecyclerViewHeaderFooter;
import com.luck.picture.lib.PictureExternalPreviewActivity;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.entity.LocalMedia;
import com.moyun.zbmy.youxian.application.Constances;
import com.moyun.zbmy.youxian.beans.ADynamicsItem;
import com.moyun.zbmy.youxian.beans.DoUpEvent;
import com.moyun.zbmy.youxian.beans.FComment;
import com.moyun.zbmy.youxian.beans.FRebellion;
import com.moyun.zbmy.youxian.beans.ImageUr;
import com.moyun.zbmy.youxian.beans.Reply;
import com.moyun.zbmy.youxian.beans.ReplyDetail;
import com.moyun.zbmy.youxian.beans.SatisfactionBean;
import com.moyun.zbmy.youxian.beans.SingleResult;
import com.moyun.zbmy.youxian.ui.adapter.CommentDetailAdapter;
import com.moyun.zbmy.youxian.ui.adapter.PicAdapter;
import com.moyun.zbmy.youxian.ui.adapter.holder.AnchorHomeDynamicsHolder;
import com.moyun.zbmy.youxian.ui.adapter.holder.ApplyHolder;
import com.moyun.zbmy.youxian.ui.util.RBaseItemDecoration;
import com.moyun.zbmy.youxian.ui.utils.GlideCircleTransform;
import com.moyun.zbmy.youxian.ui.utils.GlideUtil;
import com.moyun.zbmy.youxian.ui.utils.SkipUtil;
import com.moyun.zbmy.youxian.ui.utils.TitleUtils;
import com.moyun.zbmy.youxian.utils.UserManager;
import com.moyun.zbmy.youxian.beans.BranchBean;
import com.moyun.zbmy.youxian.beans.Reply;
import com.moyun.zbmy.youxian.beans.ReplyDetail;
import com.ruihang.generalibrary.ui.util.OnItemInternalClick;
import com.ruihang.generalibrary.ui.widget.CustomEXImageView;
import com.ruihang.generalibrary.ui.widget.CustomFontTextView;
import com.ruihang.generalibrary.ui.widget.TextViewExpandableAnimation;
import com.ruihang.generalibrary.utils.DateUtils;
import com.ruihang.generalibrary.utils.DensityUtil;
import com.ruihang.generalibrary.utils.ListUtils;
import com.ruihang.generalibrary.utils.UrlUtils;
import com.moyun.zbmy.youxian.R;
import com.moyun.zbmy.youxian.application.Constances;
import com.moyun.zbmy.youxian.base.BaseDetailActivity;
import com.moyun.zbmy.youxian.beans.ADynamicsItem;
import com.moyun.zbmy.youxian.beans.DoUpEvent;
import com.moyun.zbmy.youxian.beans.FComment;
import com.moyun.zbmy.youxian.beans.FRebellion;
import com.moyun.zbmy.youxian.beans.ImageUr;
import com.moyun.zbmy.youxian.beans.SingleResult;
import com.moyun.zbmy.youxian.http.AbsNetCallBack;
import com.moyun.zbmy.youxian.http.NetUtils;
import com.moyun.zbmy.youxian.http.ParamsEditor;
import com.moyun.zbmy.youxian.ui.adapter.CommentDetailAdapter;
import com.moyun.zbmy.youxian.ui.adapter.PicAdapter;
import com.moyun.zbmy.youxian.ui.adapter.holder.AnchorHomeDynamicsHolder;
import com.moyun.zbmy.youxian.ui.util.RBaseItemDecoration;
import com.moyun.zbmy.youxian.ui.utils.GlideCircleTransform;
import com.moyun.zbmy.youxian.ui.utils.GlideUtil;
import com.moyun.zbmy.youxian.ui.utils.SkipUtil;
import com.moyun.zbmy.youxian.ui.utils.TitleUtils;
import com.moyun.zbmy.youxian.utils.UserManager;

import org.greenrobot.eventbus.EventBus;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 民生诉求详情页
 */
public class ApplyDetailActivity extends BaseDetailActivity {

    @BindView(R.id.comment_refresh)
    protected CanRefreshLayout refreshLayout;
    @BindView(R.id.can_content_view)
    protected RecyclerView comment_list;
    @BindView(R.id.comment_footer)
    protected CanRecyclerViewHeaderFooter footer;
    @BindView(R.id.comment_foot_pb)
    protected ProgressBar pb;
    @BindView(R.id.comment_foot_loadmore)
    protected View allLoaded;
    @BindView(R.id.comment_foot_lay_normal)
    protected ViewGroup layFootNormal;
    @BindView(R.id.comment_foot_lay_empty)
    protected View layFootEmpty;
    @BindView(R.id.item_anchor_home_dynamics_img)
    protected ImageView img;
    @BindView(R.id.item_anchor_dynamics_home_name)
    protected CustomFontTextView name;
    @BindView(R.id.item_anchor_dynamics_home_time)
    protected CustomFontTextView time;
    //    @BindView(R.id.item_anchor_dynamics_home_lable)
//    protected CustomFontTextView lable;
    @BindView(R.id.item_anchor_dynamics_home_page_intro)
    protected TextView intro;
    @BindView(R.id.item_anchor_dynamic_home_pic)
    protected RecyclerView pic_list;

    @BindView(R.id.item_anchor_home_dynamics_video_rl)
    protected ViewGroup video_rl;
    @BindView(R.id.item_anchor_home_dynamics_video_coverImg)
    protected ImageView coverImage;
    @BindView(R.id.item_achor_single_temp)
    protected View singleTemp;
    @BindView(R.id.item_anchor_home_dynamics_video_play)
    protected ImageView play;
    @BindView(R.id.item_anchor_dynamics_home_page_ll_four)
    protected ViewGroup four_ll;
    @BindView(R.id.item_anchor_dynamics_home_page_img1)
    protected CustomEXImageView img1;
    @BindView(R.id.item_anchor_dynamics_home_page_img2)
    protected CustomEXImageView img2;
    @BindView(R.id.item_anchor_dynamics_home_page_img3)
    protected CustomEXImageView img3;
    @BindView(R.id.item_anchor_dynamics_home_page_img4)
    protected CustomEXImageView img4;
    @BindView(R.id.item_anchor_dynamics_home_page_img5)
    protected CustomEXImageView img5;
    @BindView(R.id.item_anchor_dynamics_home_page_img6)
    protected CustomEXImageView img6;
    @BindView(R.id.item_anchor_dynamics_home_page_rl)
    protected ViewGroup rl_type;

    @BindView(R.id.tv_anchor_up)
    protected CustomFontTextView tvUp;
    @BindView(R.id.tv_anchor_comment)
    protected CustomFontTextView tvComment;
    @BindView(R.id.item_anchor_dynamics_home_page_comment)
    protected LinearLayout llComment;
    private PicAdapter picAdapter;

    @BindView(R.id.rebllion_commit_address_title)
    protected CustomFontTextView address_cv;

    @BindView(R.id.title_top_logo_layout)
    protected LinearLayout title_top_logo_layout;
    @BindView(R.id.title_top_title)
    protected CustomFontTextView title_top_title;
    @BindView(R.id.title_top_share)
    protected CustomFontTextView title_top_share;
    @BindView(R.id.item_title)
    protected CustomFontTextView item_title;

    @BindView(R.id.tv_status)
    protected CustomFontTextView tv_status;
    @BindView(R.id.iv_up)

    public ImageView iv_up;
    @BindView(R.id.iv_up2)
    public ImageView iv_up2;

    @BindView(R.id.tv_up)
    public TextView tv_up;
    @BindView(R.id.tv_up2)
    public TextView tv_up2;

    @BindView(R.id.li_up)
    public LinearLayout li_up;
    @BindView(R.id.li_up2)
    public LinearLayout li_up2;

    private LinearLayoutManager manager;
    private CommentDetailAdapter adapter;
    private FRebellion dynamicsItem;
    //    private String id;
    private int capacity = 20;//请求数据条数
    private int indexNumber = 0;
    private int requestType = 4;// 1代表新闻评论  2专题评论 3主播评论 4报料评论
    private int commentType = 4;   //0新闻评论  1专题评论 2 直播评论 4报料评论

    private Handler handler;
    private int position;
    private boolean my = false;
    private Reply tem;

    private String replyId;
    List<Reply> reply_list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        new TitleUtils(this);
        this.setTypeName(2);//日志统计 主播圈类型
        if(null!= getIntent() && getIntent().getStringExtra("my")!=null && getIntent().getStringExtra("my").equals("my")) {
            my = true;
        }
        initView();
        getData();
    }

    @Override
    protected void initInject() {
        setContentView(R.layout.activity_apply_detail);
        butterUnbinder = ButterKnife.bind(this);
    }


    private void getData() {
        HashMap<String, Object> param = new HashMap<>();
        param.put("infoId",dynamicsItem.getInfoId());

        if (UserManager.isLoginS()) {
            param.put("userId", UserManager.getInstance().getUser().getUserId());
        }

        NetUtils.getNetAdapter().getReplyDetail(getOwnerName(),param, new AbsNetCallBack<ReplyDetail>(ReplyDetail.class) {
            @Override
            public void onSuc(ReplyDetail fRebellions) {
                reply_list = fRebellions.getReply();

                initInfo(fRebellions.getReply());

                replyId = fRebellions.getPolitickReplySatisfactionMap().getReplyId();

                if(fRebellions.getPolitickReplySatisfactionMap()!=null){


                    if(!TextUtils.isEmpty(fRebellions.getPolitickReplySatisfactionMap().getSatisfactionStatus())) {
                        if (fRebellions.getPolitickReplySatisfactionMap().getSatisfactionStatus().equals("1")) {
                            iv_up.setBackgroundResource(R.mipmap.icon_compere_btn_press);
                            iv_up2.setBackgroundResource(R.mipmap.icon_trample_btn_normal);

                        } else if (fRebellions.getPolitickReplySatisfactionMap().getSatisfactionStatus().equals("0")) {
                            iv_up.setBackgroundResource(R.mipmap.icon_compere_btn_normal);
                            iv_up2.setBackgroundResource(R.mipmap.icon_trample_btn_press);

                        } else {
                            iv_up.setBackgroundResource(R.mipmap.icon_compere_btn_normal);
                            iv_up2.setBackgroundResource(R.mipmap.icon_trample_btn_normal);
                        }
                    }else {
                        iv_up.setBackgroundResource(R.mipmap.icon_compere_btn_normal);
                        iv_up2.setBackgroundResource(R.mipmap.icon_trample_btn_normal);
                    }



                    if(!TextUtils.isEmpty(fRebellions.getPolitickReplySatisfactionMap().getPolitickReplySatisfactionRate().getSatisfactionRate()))
                    {
                        tv_up.setText((int)(100*Double.valueOf(fRebellions.getPolitickReplySatisfactionMap().getPolitickReplySatisfactionRate().getSatisfactionRate())) +"%");
                    }else{
                        tv_up.setText("0%");

                    }

                    if(!TextUtils.isEmpty(fRebellions.getPolitickReplySatisfactionMap().getPolitickReplySatisfactionRate().getNotSatisfactionRate()))
                    {
                        tv_up2.setText((int)(100*Double.valueOf(fRebellions.getPolitickReplySatisfactionMap().getPolitickReplySatisfactionRate().getNotSatisfactionRate())) +"%");
                    }else{
                        tv_up2.setText("0%");
                    }

                }



            }

            @Override
            public void onError(Throwable ex, String errorMsg) {
                toast(R.string.loading_no_data);
            }

            @Override
            public void onEnd() {

            }
        });

    }

    private void initView() {
        title_top_logo_layout.setVisibility(View.GONE);
        title_top_title.setVisibility(View.VISIBLE);
        if(my) {
            title_top_title.setText(getText(R.string.tv_my_apply));
        }else{
            title_top_title.setText(getText(R.string.tv_apply));
        }

        //   title_top_share.setVisibility(View.VISIBLE);

        handler = new Handler();
        manager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        comment_list.setItemAnimator(new DefaultItemAnimator());
        comment_list.setLayoutManager(manager);
        comment_list.addItemDecoration(new RBaseItemDecoration(DensityUtil.dip2px(this, 1),
                ContextCompat.getColor(this, R.color.colorItemListDivider)));
        Intent intent = getIntent();
        if (intent == null) {
            toast(R.string.data_wrong);
            finish();
            return;
        }
        dynamicsItem = (FRebellion) intent.getSerializableExtra(EX_DATA);
        initTop(dynamicsItem);
        requestType = getIntent().getIntExtra(EX_TYPE_NEWS, 4);
        commentType = getIntent().getIntExtra(EX_COMMENT_TYPE, 4);
        position =  getIntent().getIntExtra(DO_UP_POS, -1);


    }

    @OnClick(value = {R.id.comment_write_comment})
    public void commentClick(View view) {
        showCommentDialog(dynamicsItem.getInfoId(), "", commentType);  //0代表新闻评论
    }


    private void initInfo(List<Reply> datas) {
        if (ListUtils.isListValued(datas)) {
            llComment.setVisibility(View.VISIBLE);
            llComment.removeAllViews();
            LayoutInflater inflater = LayoutInflater.from(ApplyDetailActivity.this);

            for(final Reply ff : datas){
                RelativeLayout root = (RelativeLayout) inflater.inflate(R.layout.item_reply, null);


//                if(!my)
//                {
//                    (root.findViewById(R.id.li_reply)).setVisibility(View.GONE);
//
//                }else{
//                    (root.findViewById(R.id.li_reply)).setVisibility(View.VISIBLE);
//
//                }


                (root.findViewById(R.id.li_reply)).setVisibility(View.GONE);




                //    0.不满意 1.满意   -1. 还原

                if(ff.getSatisfacation()==0){
                    Drawable drawable = getResources().getDrawable(R.mipmap.manyi);
                    ((CustomFontTextView)(root.findViewById(R.id.tv_manyi))).setDrawables(drawable, null, null, null);
                    ((CustomFontTextView)(root.findViewById(R.id.tv_manyi))).setTextColor(getResources().getColor(R.color.color_b2b2b2));

                    Drawable drawable2 = getResources().getDrawable(R.mipmap.bumanyi_xuanzhong);
                    ((CustomFontTextView)(root.findViewById(R.id.tv_bumanyi))).setDrawables(drawable2, null, null, null);
                    ((CustomFontTextView)(root.findViewById(R.id.tv_bumanyi))).setTextColor(getResources().getColor(R.color.colorBlack22));

                }else  if(ff.getSatisfacation()==1){
                    Drawable drawable = getResources().getDrawable(R.mipmap.manyi_xuanzhong);
                    ((CustomFontTextView)(root.findViewById(R.id.tv_manyi))).setDrawables(drawable, null, null, null);
                    ((CustomFontTextView)(root.findViewById(R.id.tv_manyi))).setTextColor(getResources().getColor(R.color.blue_main));

                    Drawable drawable2 = getResources().getDrawable(R.mipmap.bumanyi);
                    ((CustomFontTextView)(root.findViewById(R.id.tv_bumanyi))).setDrawables(drawable2, null, null, null);
                    ((CustomFontTextView)(root.findViewById(R.id.tv_bumanyi))).setTextColor(getResources().getColor(R.color.color_b2b2b2));

                }else {
                    Drawable drawable = getResources().getDrawable(R.mipmap.manyi);
                    ((CustomFontTextView)(root.findViewById(R.id.tv_manyi))).setDrawables(drawable, null, null, null);
                    ((CustomFontTextView)(root.findViewById(R.id.tv_manyi))).setTextColor(getResources().getColor(R.color.color_b2b2b2));

                    Drawable drawable2 = getResources().getDrawable(R.mipmap.bumanyi);
                    ((CustomFontTextView)(root.findViewById(R.id.tv_bumanyi))).setDrawables(drawable2, null, null, null);
                    ((CustomFontTextView)(root.findViewById(R.id.tv_bumanyi))).setTextColor(getResources().getColor(R.color.color_b2b2b2));

                }


                //    0.不满意 1.满意   -1. 还原

                (root.findViewById(R.id.tv_manyi)).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if(ff.getSatisfacation()==1){
                            submitSatisfacation(ff, -1);
                        }else{
                            submitSatisfacation(ff, 1);
                        }
                    }
                });


                (root.findViewById(R.id.tv_bumanyi)).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if(ff.getSatisfacation()==0){
                            submitSatisfacation(ff, -1);
                        }else{
                            submitSatisfacation(ff, 0);
                        }
                    }
                });




                ((TextView)root.findViewById(R.id.time)).setText(TextUtils.isEmpty(ff.getReplyDate()) ? "" :ff.getReplyDate());
                ((TextView)root.findViewById(R.id.content)).setText(TextUtils.isEmpty(ff.getReplyContent()) ? "" :ff.getReplyContent());
                llComment.addView(root);
            }


        } else {
            tvComment.setText("");
            llComment.setVisibility(View.GONE);
        }

    }




    private void submitSatisfacation(Reply reply, final int isSatisfied){
//        if (!UserManager.isLoginS()) {
//            toLogin();
//            return ;
//        }
//
//        tem = reply;
//
//        ArrayMap<String, Object> p = new ArrayMap<>();
//        p.put("isSatisfied",isSatisfied); //    0.不满意 1.满意   -1. 还原
//        p.put("replyId", reply.getId());
//        p.put("token", UserManager.getInstance().getUser().getToken());
//        NetUtils.getNetAdapter().submitSatisfacation(getOwnerName(), p, new AbsNetCallBack<SingleResult>(SingleResult.class) {
//            @Override
//            public void onSuc(SingleResult result) {
//                tem.setSatisfacation(isSatisfied);
//                reSetList(tem);
//            }
//
//            @Override
//            public void onEnd() {
//                super.onEnd();
//
//            }
//
//            @Override
//            public void onError(Throwable ex, String errorMsg) {
//                super.onError(ex, errorMsg);
//            }
//        });
    }



    private void reSetList(Reply item){
        for(int i =0;i< reply_list.size();i++){
            if(reply_list.get(i).getId()==item.getId()){
                reply_list.set(i,item);
            }
        }
        initInfo(reply_list);
    }


    private OnItemInternalClick commentItemClick = new OnItemInternalClick() {
        @Override
        public void onItemInternalClick(View itemView, View view, int position) {
            FComment comment = (FComment) adapter.getItem(position);
            if (!TextUtils.isEmpty(comment.getCommentId())) {
                showCommentDialog(dynamicsItem.getInfoId(), comment.getCommentId(), commentType);
            } else {
                toast("评论id为空");
            }
        }
    };

    OnItemInternalClick internalClick = new OnItemInternalClick() {
        @Override
        public void onItemInternalClick(View itemView, View view, int position) {
            switch (view.getId()) {
                case R.id.tv_anchor_up:
                    if (!UserManager.isLoginS()) {
                        toLogin();
                        return;
                    }
                    doUp();
                    break;
                case R.id.item_anchor_home_dynamics_video_play:
                    ADynamicsItem dynamics = (ADynamicsItem) adapter.getItem(position);
                    String contentVideoUrl = UrlUtils.linkUrls(Constances.ST_BASE_URL, dynamics.getContentVideo());
                    SkipUtil.skipToVideoPlay(ApplyDetailActivity.this, contentVideoUrl, dynamics.getPlayedPos(), position);
                    break;
            }
        }
    };

    private AnchorHomeDynamicsHolder lastHolder = null;
    Drawable showIcon = null;
    private void doUp() {
        tvUp.setDrawableEnlarge();

        ParamsEditor pe = new ParamsEditor();
        pe.put("dynamicId", dynamicsItem.getInfoId());
        pe.put("userId", UserManager.getInstance().getUser().getUserId());
        pe.put("fType", 101);


        NetUtils.getNetAdapter().doUpForDynamics(getOwnerName(), pe.getEncryptedParams(UserManager.getInstance().getToken()), new AbsNetCallBack<SingleResult>(SingleResult.class) {
            @Override
            public void onSuc(SingleResult singleResult) {
                if (singleResult.isSuccess()) {
                    if (null != dynamicsItem) {
                        if (dynamicsItem.getIsDigg()) {
                            dynamicsItem.setIsDigg(!dynamicsItem.getIsDigg());
                            dynamicsItem.setCountDigg(dynamicsItem.getCountDigg() - 1);
                            EventBus.getDefault().post(new DoUpEvent(position,dynamicsItem.getIsDigg(),dynamicsItem.getCountDigg()));

                        } else {
                            dynamicsItem.setIsDigg(!dynamicsItem.getIsDigg());
                            dynamicsItem.setCountDigg(dynamicsItem.getCountDigg() + 1);
                            EventBus.getDefault().post(new DoUpEvent(position,dynamicsItem.getIsDigg(),dynamicsItem.getCountDigg()));
                        }

                        if (dynamicsItem.getIsDigg()) {
                            Drawable drawable = ApplyDetailActivity.this.getResources().getDrawable(R.mipmap.icon_compere_btn_press);
                            drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
                            tvUp.setDrawables(drawable, null, null, null);
                            tvUp.setTextColor(ApplyDetailActivity.this.getResources().getColor(R.color.colorTxRedMain_));
                            showIcon = drawable;
                        } else {
                            Drawable drawable = ApplyDetailActivity.this.getResources().getDrawable(R.mipmap.icon_compere_btn_normal);
                            drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
                            tvUp.setDrawables(drawable, null, null, null);
                            tvUp.setTextColor(ApplyDetailActivity.this.getResources().getColor(R.color.colorOpinionSelectNo));
                            showIcon = drawable;
                        }
                        int upCount = dynamicsItem.getCountDigg();
                        if (upCount > 0) {
                            tvUp.setText(String.valueOf(upCount));
                        } else {
                            tvUp.setText("点赞");
                        }
                        if (dynamicsItem.getIsDigg()) {
                            addInvitation(Constances.ADD_INTEGRAL_DOUP, false);
                        }
                    }

                }

            }

        });




    }


    public void initTop(final FRebellion aDynamicsItem) {

        item_title.setText(!TextUtils.isEmpty(aDynamicsItem.getDiscloseTitle()) ? aDynamicsItem.getDiscloseTitle() : "");

        if(aDynamicsItem.getIsAnon()==1){
            name.setText(getText(R.string.tv_apply_person)+": "+ getText(R.string.tv_apply_person_annoy));

        }else{
            name.setText(getText(R.string.tv_apply_person)+": "+ (!TextUtils.isEmpty(aDynamicsItem.getUserName()) ? aDynamicsItem.getUserName() : ""));

        }


        if(aDynamicsItem.getAccessStatus()==3){
            tv_status.setText(getResources().getText(R.string.tv_done));
            tv_status.setTextColor(getResources().getColor(R.color.color_FF35C4FF));
            tv_status.setVisibility(View.VISIBLE);
        }else if(aDynamicsItem.getAccessStatus()==2){
            tv_status.setText(aDynamicsItem.getBranch().branchName+ getResources().getText(R.string.tv_doing));
            tv_status.setTextColor(getResources().getColor(R.color.colorTxRedMain_));
            tv_status.setVisibility(View.VISIBLE);
        }else if(aDynamicsItem.getAccessStatus()==1){
            tv_status.setText(getResources().getText(R.string.tv_wait));
            tv_status.setTextColor(getResources().getColor(R.color.colorTxRedMain_));
            tv_status.setVisibility(View.VISIBLE);
        }else {
            tv_status.setVisibility(View.GONE);
        }


        time.setText(!TextUtils.isEmpty(aDynamicsItem.getPubTime()) ? DateUtils.timeAgo(DateUtils.getDataDate(aDynamicsItem)) : "");
        intro.setText((TextUtils.isEmpty(aDynamicsItem.getDiscloseText()) ? "" : aDynamicsItem.getDiscloseText()));
//        intro.setText(!TextUtils.isEmpty(aDynamicsItem.getDiscloseText()) ? aDynamicsItem.getDiscloseText() : "");
        address_cv.setText(aDynamicsItem.getLocation());

        if(TextUtils.isEmpty(aDynamicsItem.getLocation())){
            address_cv.setVisibility(View.GONE);
        }else{
            address_cv.setVisibility(View.VISIBLE);
        }
        address_cv.setVisibility(View.GONE);


        int count = aDynamicsItem.getCountComment();
        if (count > 0) {
            tvComment.setText(String.valueOf(count));
        } else {
            tvComment.setText("");
        }

        if (!aDynamicsItem.getIsDigg()) {
            Drawable drawable = getResources().getDrawable(R.mipmap.icon_compere_btn_normal);
            drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
            tvUp.setDrawables(drawable, null, null, null);
            tvUp.setTextColor(getResources().getColor(R.color.colorOpinionSelectNo));
        } else {
            Drawable drawable = getResources().getDrawable(R.mipmap.icon_compere_btn_press);
            drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
            tvUp.setDrawables(drawable, null, null, null);
            tvUp.setTextColor(getResources().getColor(R.color.colorTxRedMain_));
        }
        int upCount = aDynamicsItem.getCountDigg();
        if (upCount > 0) {
            tvUp.setText(String.valueOf(upCount));
        } else {
            tvUp.setText("");
        }
        tvUp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (internalClick != null) {
                    if (!UserManager.isLoginS()) {
                        toLogin();
                        return;
                    }
                    doUp();
                }
            }
        });
        tvComment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (internalClick != null) {
                    //            internalClick.onItemInternalClick(itemView, v, getAdapterPosition());
                }
            }
        });

        li_up.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!UserManager.isLoginS()) {
                    toLogin();
                    return;
                }
                if (dynamicsItem.getPolitickReplySatisfactionMap().getSatisfactionStatus().equals("1")) {
                    doUpNew(aDynamicsItem, position, -1);

                } else {
                    doUpNew(aDynamicsItem, position, 1);

                }
            }

        });
        li_up2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!UserManager.isLoginS()) {
                    toLogin();
                    return;
                }
                if (dynamicsItem.getPolitickReplySatisfactionMap().getSatisfactionStatus().equals("0")) {
                    doUpNew(aDynamicsItem, position, -1);

                }else {
                    doUpNew(aDynamicsItem, position, 0);
                }
            }

        });



        play.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String contentVideoUrl = UrlUtils.linkUrls(Constances.ST_BASE_URL, dynamicsItem.getDiscloseVideo());
                SkipUtil.skipToVideoPlay(ApplyDetailActivity.this, contentVideoUrl, 0, 0);
            }
        });

        if (!TextUtils.isEmpty(aDynamicsItem.getUserAvator())) {
            GlideUtil.getGlid(ApplyDetailActivity.this, aDynamicsItem.getUserAvator()).error(R.mipmap.icon_circle_avatar)
                    .placeholder(R.mipmap.icon_circle_avatar).transform(new GlideCircleTransform(ApplyDetailActivity.this))
                    .into(img);
        } else {
            img.setImageResource(R.mipmap.icon_circle_avatar);
        }
        int pa = getResources().getDimensionPixelOffset(R.dimen.item_list_large_margin_top);
        if (TextUtils.isEmpty(aDynamicsItem.getDiscloseVideo()) && !ListUtils.isListValued(aDynamicsItem.getDiscloseImage())) {
            rl_type.setVisibility(View.GONE);
        } else {
            rl_type.setVisibility(View.VISIBLE);
        }
        if (TextUtils.isEmpty(aDynamicsItem.getDiscloseVideo())) {
            play.setVisibility(View.GONE);
            coverImage.setVisibility(View.GONE);

            if (ListUtils.isListValued(aDynamicsItem.getDiscloseImage())) {
                if (aDynamicsItem.getDiscloseImage().size() == 1) {
                    pic_list.setVisibility(View.GONE);
                    video_rl.setVisibility(View.VISIBLE);
                    coverImage.setVisibility(View.VISIBLE);
                    four_ll.setVisibility(View.GONE);
//                    singleImg.setVisibility(View.VISIBLE);
//                    GlideUtil.getGlideWithSmall169Def(context, contentImage.get(0).getImageUrl()).into(singleImg);
                    GlideUtil.getGlideWith11DefNoFit(ApplyDetailActivity.this, aDynamicsItem.getDiscloseImage().get(0).getImageUrl())
//                            .fitCenter()
//                            .listener(lis)
                            .into(coverImage);
                } else if (aDynamicsItem.getDiscloseImage().size() == 4) {
                    pic_list.setVisibility(View.GONE);
                    video_rl.setVisibility(View.GONE);
                    four_ll.setVisibility(View.VISIBLE);
                    GlideUtil.loadImgWith11Def(ApplyDetailActivity.this, aDynamicsItem.getDiscloseImage().get(0).getImageUrl(), img1);
                    GlideUtil.loadImgWith11Def(ApplyDetailActivity.this, aDynamicsItem.getDiscloseImage().get(1).getImageUrl(), img2);
                    GlideUtil.loadImgWith11Def(ApplyDetailActivity.this, aDynamicsItem.getDiscloseImage().get(2).getImageUrl(), img4);
                    GlideUtil.loadImgWith11Def(ApplyDetailActivity.this, aDynamicsItem.getDiscloseImage().get(3).getImageUrl(), img5);
                    img3.setImageDrawable(null);
                    img6.setImageDrawable(null);
                } else {
                    initView(aDynamicsItem.getDiscloseImage());
                    pic_list.setVisibility(View.VISIBLE);
                    video_rl.setVisibility(View.GONE);
                    four_ll.setVisibility(View.GONE);
                }
            } else {
                pic_list.setVisibility(View.GONE);
                video_rl.setVisibility(View.GONE);
                four_ll.setVisibility(View.GONE);
//                singleImg.setVisibility(View.GONE);
            }
        } else {
            pic_list.setVisibility(View.GONE);
            four_ll.setVisibility(View.GONE);
//            singleImg.setVisibility(View.GONE);
            video_rl.setVisibility(View.VISIBLE);
            coverImage.setVisibility(View.VISIBLE);
            play.setVisibility(View.VISIBLE);
            String coverImageUrl = aDynamicsItem.getCoverImage();
//            DrawableRequestBuilder<String> drb=
            GlideUtil.getGlideWithMid169DefNoFit(ApplyDetailActivity.this, coverImageUrl)
//                            .fitCenter()
//                            .listener(lis)
                    .into(coverImage);

        }
    }


    private void doUpNew(final FRebellion aDynamicsItem, final int position,final int isSatisfied) {


        ArrayMap<String, Object> p = new ArrayMap<>();
        p.put("infoId", aDynamicsItem.getInfoId());
        p.put("userId", UserManager.getInstance().getUser().getUserId());

        p.put("isSatisfied",isSatisfied); //    0.不满意 1.满意   -1. 还原
        if(aDynamicsItem.getPolitickReplySatisfactionMap()!=null && !TextUtils.isEmpty(aDynamicsItem.getPolitickReplySatisfactionMap().getReplyId())) {
            p.put("replyId", aDynamicsItem.getPolitickReplySatisfactionMap().getReplyId());
        }else{
            p.put("replyId", "");

        }
        p.put("token", UserManager.getInstance().getUser().getToken());
        NetUtils.getNetAdapter().submitSatisfacation(getOwnerName(), p, new AbsNetCallBack<SatisfactionBean>(SatisfactionBean.class) {
            @Override
            public void onSuc(SatisfactionBean result) {

                if(null !=result && result.getPolitickReplySatisfactionRate()!=null) {

                    EventBus.getDefault().post(new DoUpEvent(position,dynamicsItem.getIsDigg(),dynamicsItem.getCountDigg(),result));

                    dynamicsItem.getPolitickReplySatisfactionMap().setSatisfactionStatus(result.getSatisfactionStatus());

                    replyId = result.getReplyId();

                    if(!TextUtils.isEmpty(result.getSatisfactionStatus())) {

                        if(result.getSatisfactionStatus().equals("1")){
                            iv_up.setBackgroundResource(R.mipmap.icon_compere_btn_press);
                            iv_up2.setBackgroundResource(R.mipmap.icon_trample_btn_normal);
                        }else if(result.getSatisfactionStatus().equals("0")){
                            iv_up.setBackgroundResource(R.mipmap.icon_compere_btn_normal);
                            iv_up2.setBackgroundResource(R.mipmap.icon_trample_btn_press);

                        }else if(result.getSatisfactionStatus().equals("-1")){
                            iv_up.setBackgroundResource(R.mipmap.icon_compere_btn_normal);
                            iv_up2.setBackgroundResource(R.mipmap.icon_trample_btn_normal);
                        }
                    }else{
                        iv_up.setBackgroundResource(R.mipmap.icon_compere_btn_normal);
                        iv_up2.setBackgroundResource(R.mipmap.icon_trample_btn_normal);
                    }




                    if(!TextUtils.isEmpty(result.getPolitickReplySatisfactionRate().getSatisfactionRate()))
                        {
                            tv_up.setText((int)(100*Double.valueOf(result.getPolitickReplySatisfactionRate().getSatisfactionRate())) +"%");
                        }else{
                            tv_up.setText("0%");

                        }

                        if(!TextUtils.isEmpty(result.getPolitickReplySatisfactionRate().getNotSatisfactionRate()))
                        {
                            tv_up2.setText((int)(100*Double.valueOf(result.getPolitickReplySatisfactionRate().getNotSatisfactionRate())) +"%");
                        }else{
                            tv_up2.setText("0%");

                        }

                }
            }

            @Override
            public void onEnd() {
                super.onEnd();

            }

            @Override
            public void onError(Throwable ex, String errorMsg) {
                super.onError(ex, errorMsg);
            }
        });


    }




    private void initView(List<ImageUr> dataImgs) {
        manager = new GridLayoutManager(ApplyDetailActivity.this, 3);
        pic_list.setItemAnimator(new DefaultItemAnimator());
        pic_list.setLayoutManager(manager);
        manager.setSmoothScrollbarEnabled(true);
        manager.setAutoMeasureEnabled(true);
        pic_list.setHasFixedSize(true);
        pic_list.setNestedScrollingEnabled(false);
        if (picAdapter == null) {
            // new UiListMarginUtils().setMargin(context, pic_list, R.dimen.item_list_large_margin_left, R.dimen.item_list_large_margin_right);
            picAdapter = new PicAdapter(ApplyDetailActivity.this, dataImgs);
        } else {
            picAdapter.setData(dataImgs);
        }
        pic_list.setAdapter(picAdapter);
    }

    @OnClick(value = {R.id.item_anchor_dynamics_home_page_img1, R.id.item_anchor_dynamics_home_page_img2,
            R.id.item_anchor_dynamics_home_page_img4, R.id.item_anchor_dynamics_home_page_img5})
    public void imgClick(View view) {
        Intent intent = new Intent(ApplyDetailActivity.this, PictureExternalPreviewActivity.class);
        switch (view.getId()) {
            case R.id.item_anchor_dynamics_home_page_img1:
                intent.putExtra(PictureConfig.EXTRA_POSITION, 0);
                break;
            case R.id.item_anchor_dynamics_home_page_img2:
                intent.putExtra(PictureConfig.EXTRA_POSITION, 1);
                break;
            case R.id.item_anchor_dynamics_home_page_img4:
                intent.putExtra(PictureConfig.EXTRA_POSITION, 2);
                break;
            case R.id.item_anchor_dynamics_home_page_img5:
                intent.putExtra(PictureConfig.EXTRA_POSITION, 3);

                break;

        }
        List<LocalMedia> images = new ArrayList<LocalMedia>();
        for (ImageUr imageUr : dynamicsItem.getDiscloseImage()) {
            LocalMedia localMedia = new LocalMedia();
            localMedia.setPath(Constances.ST_BASE_URL + imageUr.getImageUrl());
            images.add(localMedia);
        }

        intent.putExtra("type", 1);
        intent.putExtra(PictureConfig.EXTRA_PREVIEW_SELECT_LIST, (Serializable) images);
        startActivity(intent);

    }
    @OnClick({R.id.item_anchor_home_single, R.id.item_anchor_home_dynamics_video_coverImg, R.id.item_anchor_home_dynamics_coverImg})
    public void onlyImgClick(View view) {
        if (dynamicsItem == null || !TextUtils.isEmpty(dynamicsItem.getDiscloseVideo())
                || dynamicsItem.getDiscloseImage() == null || dynamicsItem.getDiscloseImage().size() == 0
                || dynamicsItem.getDiscloseImage().get(0) == null || TextUtils.isEmpty(dynamicsItem.getDiscloseImage().get(0).getImageUrl())) {
            return;
        }
        String path = UrlUtils.linkUrls(Constances.ST_BASE_URL, dynamicsItem.getDiscloseImage().get(0).getImageUrl());
        String type = (path.endsWith(".gif") || path.endsWith(".GIF")) ? "image/gif" : "image";
        Intent intent = new Intent(ApplyDetailActivity.this, PictureExternalPreviewActivity.class);
        intent.putExtra("type", 1);
        ArrayList<LocalMedia> list = new ArrayList<>();
        list.add(new LocalMedia(path, "", type));
        intent.putExtra(PictureConfig.EXTRA_PREVIEW_SELECT_LIST, list);
        intent.putExtra(PictureConfig.EXTRA_POSITION, 0);
        startActivity(intent);
    }

    @OnClick(value = {R.id.item_anchor_home_dynamics_video_play})
    public void playClick(View view) {
        SkipUtil.skipToVideoPlay(this, UrlUtils.linkUrls(Constances.ST_BASE_URL, dynamicsItem.getDiscloseVideo())
                , dynamicsItem.getPlayedPos(), 0);

    }


}
